using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class MGiaoVienDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the mGiaoVien table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, MGiaoVienDTO mGiaoVien)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mGiaoVien", mGiaoVien);

			object[] parameters = new object[]
			{
				Null.GetNull(mGiaoVien.Gv_ten, ""),
				Null.GetNull(mGiaoVien.Gv_ngaysinh, DBNull.Value),
				Null.GetNull(mGiaoVien.CB_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.CV_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.KH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_ngaynhapngu, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_cohuu, false),
				Null.GetNull(mGiaoVien.Gv_thinhgiang, false),
				Null.GetNull(mGiaoVien.Gv_quequan, ""),
				Null.GetNull(mGiaoVien.Gv_hokhau, ""),
				Null.GetNull(mGiaoVien.TDGV_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.CN_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.NVSP_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.NN_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.TH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_trinhdoct, ""),
				Null.GetNull(mGiaoVien.SH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_socmt, ""),
				Null.GetNull(mGiaoVien.Gv_noicap, ""),
				Null.GetNull(mGiaoVien.Gv_ngaycapcmt, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_coquan, ""),
				Null.GetNull(mGiaoVien.Gv_ghichu, "")
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mGiaoVienInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the mGiaoVien table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, MGiaoVienDTO mGiaoVien)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mGiaoVien", mGiaoVien);

			object[] parameters = new object[]
			{
				Null.GetNull(mGiaoVien.GV_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_ten, ""),
				Null.GetNull(mGiaoVien.Gv_ngaysinh, DBNull.Value),
				Null.GetNull(mGiaoVien.CB_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.CV_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.KH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_ngaynhapngu, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_cohuu, false),
				Null.GetNull(mGiaoVien.Gv_thinhgiang, false),
				Null.GetNull(mGiaoVien.Gv_quequan, ""),
				Null.GetNull(mGiaoVien.Gv_hokhau, ""),
				Null.GetNull(mGiaoVien.TDGV_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.CN_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.NVSP_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.NN_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.TH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_trinhdoct, ""),
				Null.GetNull(mGiaoVien.SH_ID, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_socmt, ""),
				Null.GetNull(mGiaoVien.Gv_noicap, ""),
				Null.GetNull(mGiaoVien.Gv_ngaycapcmt, DBNull.Value),
				Null.GetNull(mGiaoVien.Gv_coquan, ""),
				Null.GetNull(mGiaoVien.Gv_ghichu, "")
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mGiaoVienUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mGiaoVien table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int gV_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(gV_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mGiaoVienDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mGiaoVien table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mGiaoVienDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mGiaoVienDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the mGiaoVien table.
		/// </summary>
		public static MGiaoVienDTO Select(Database objDb, int gV_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(gV_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the mGiaoVien table.
		/// </summary>
		public static DataTable Select(Database objDb, int gV_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(gV_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the mGiaoVien table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("mGiaoVienSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the mGiaoVien table.
		/// </summary>
		public static List<MGiaoVienDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mGiaoVien table.
		/// </summary>
		public static List<MGiaoVienDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mGiaoVien table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mGiaoVien table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mGiaoVienSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the MGiaoVienDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static MGiaoVienDTO MapDataReader(IDataReader dataReader)
		{
			MGiaoVienDTO mGiaoVienDTO = new MGiaoVienDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return mGiaoVienDTO;
			mGiaoVienDTO.GV_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["GV_ID"]);
			mGiaoVienDTO.Gv_ten = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_ten"]);
			mGiaoVienDTO.Gv_ngaysinh = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["gv_ngaysinh"]);
			mGiaoVienDTO.CB_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["CB_ID"]);
			mGiaoVienDTO.CV_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["CV_ID"]);
			mGiaoVienDTO.KH_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["KH_ID"]);
			mGiaoVienDTO.Gv_ngaynhapngu = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["gv_ngaynhapngu"]);
			mGiaoVienDTO.Gv_cohuu = clsCommon.fncCnvNullToBool(dt.Rows[0]["gv_cohuu"]);
			mGiaoVienDTO.Gv_thinhgiang = clsCommon.fncCnvNullToBool(dt.Rows[0]["gv_thinhgiang"]);
			mGiaoVienDTO.Gv_quequan = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_quequan"]);
			mGiaoVienDTO.Gv_hokhau = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_hokhau"]);
			mGiaoVienDTO.TDGV_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["TDGV_ID"]);
			mGiaoVienDTO.CN_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["CN_ID"]);
			mGiaoVienDTO.NVSP_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["NVSP_ID"]);
			mGiaoVienDTO.NN_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["NN_ID"]);
			mGiaoVienDTO.TH_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["TH_ID"]);
			mGiaoVienDTO.Gv_trinhdoct = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_trinhdoct"]);
			mGiaoVienDTO.SH_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["SH_ID"]);
			mGiaoVienDTO.Gv_socmt = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_socmt"]);
			mGiaoVienDTO.Gv_noicap = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_noicap"]);
			mGiaoVienDTO.Gv_ngaycapcmt = clsCommon.fncCnvNullToDatetime(dt.Rows[0]["gv_ngaycapcmt"]);
			mGiaoVienDTO.Gv_coquan = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_coquan"]);
			mGiaoVienDTO.Gv_ghichu = clsCommon.fncCnvNullToString(dt.Rows[0]["gv_ghichu"]);

			return mGiaoVienDTO;
		}
		/// <summary>
		/// Creates a new instance of the MGiaoVienDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<MGiaoVienDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<MGiaoVienDTO> lstResult = new List<MGiaoVienDTO>();
			MGiaoVienDTO mGiaoVienDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			mGiaoVienDTO = new MGiaoVienDTO();
			mGiaoVienDTO.GV_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["GV_ID"]);
			mGiaoVienDTO.Gv_ten = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_ten"]);
			mGiaoVienDTO.Gv_ngaysinh = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["gv_ngaysinh"]);
			mGiaoVienDTO.CB_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["CB_ID"]);
			mGiaoVienDTO.CV_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["CV_ID"]);
			mGiaoVienDTO.KH_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["KH_ID"]);
			mGiaoVienDTO.Gv_ngaynhapngu = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["gv_ngaynhapngu"]);
			mGiaoVienDTO.Gv_cohuu = clsCommon.fncCnvNullToBool(dt.Rows[i]["gv_cohuu"]);
			mGiaoVienDTO.Gv_thinhgiang = clsCommon.fncCnvNullToBool(dt.Rows[i]["gv_thinhgiang"]);
			mGiaoVienDTO.Gv_quequan = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_quequan"]);
			mGiaoVienDTO.Gv_hokhau = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_hokhau"]);
			mGiaoVienDTO.TDGV_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["TDGV_ID"]);
			mGiaoVienDTO.CN_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["CN_ID"]);
			mGiaoVienDTO.NVSP_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["NVSP_ID"]);
			mGiaoVienDTO.NN_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["NN_ID"]);
			mGiaoVienDTO.TH_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["TH_ID"]);
			mGiaoVienDTO.Gv_trinhdoct = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_trinhdoct"]);
			mGiaoVienDTO.SH_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["SH_ID"]);
			mGiaoVienDTO.Gv_socmt = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_socmt"]);
			mGiaoVienDTO.Gv_noicap = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_noicap"]);
			mGiaoVienDTO.Gv_ngaycapcmt = clsCommon.fncCnvNullToDatetime(dt.Rows[i]["gv_ngaycapcmt"]);
			mGiaoVienDTO.Gv_coquan = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_coquan"]);
			mGiaoVienDTO.Gv_ghichu = clsCommon.fncCnvNullToString(dt.Rows[i]["gv_ghichu"]);
lstResult.Add(mGiaoVienDTO);
}

			return lstResult;
		}

		#endregion
	}
}
